package com.mmy.service.impl.hotel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.dao.hotel.THotelOrderRepository;
import com.mmy.dao.hotel.THotelPlanRepository;
import com.mmy.dao.sys.TSysTogetherRepository;
import com.mmy.dao.sys.TSysUserRepository;
import com.mmy.domain.hotel.THotelOrder;
import com.mmy.domain.hotel.THotelPlan;
import com.mmy.domain.sys.TSysTogether;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.ServiceCommon;
import com.mmy.service.hotel.HotelOrderService;

@Service
public class HotelOrderServiceImpl implements HotelOrderService {

	@Autowired
	THotelOrderRepository tHotelOrderRepository;
	@Autowired
	ServiceCommon common;
	@Autowired
	THotelPlanRepository tHotelPlanRepository;
	@Autowired
	TSysUserRepository tSysUserRepository;
	@Autowired
	TSysTogetherRepository tSysTogetherRepository;

	/**
	 * 个人中心通过登录人获取酒店预留 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapHotelReserve(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, tHotelOrderRepository.countByUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取酒店退单 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapHotelRefund(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, tHotelOrderRepository.countByRefundUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取酒店订单 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapHotelOrder(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, tHotelOrderRepository.countByOrderUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获酒店房间预留
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<THotelOrder> findByUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<THotelOrder> lstHotelOrder = new ArrayList<THotelOrder>();
		lstHotelOrder = tHotelOrderRepository.findByUserID(iUserID, iTempPageNo, iPageSize);
		return lstHotelOrder;
	}

	/**
	 * 个人中心通过登录人获取酒店房间订单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<THotelOrder> findByOrderUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<THotelOrder> lstHotelOrder = new ArrayList<THotelOrder>();
		lstHotelOrder = tHotelOrderRepository.findByOrderUserID(iUserID, iTempPageNo, iPageSize);
		return lstHotelOrder;
	}

	/**
	 * 个人中心通过登录人获取酒店房间退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<THotelOrder> findByChargeBackUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<THotelOrder> lstHotelOrder = new ArrayList<THotelOrder>();
		lstHotelOrder = tHotelOrderRepository.findByChargeBackUserID(iUserID, iTempPageNo, iPageSize);
		return lstHotelOrder;
	}

	/**
	 * 个人中心通过登录人取消房间预留
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		THotelOrder dbHotelOrder = tHotelOrderRepository.findByID(iID);
		dbHotelOrder.setOrderState((byte) 3);
		dbHotelOrder.setModifyDate(new Date());
		dbHotelOrder.setReviser(sessionSysUser.getUserName());
		tHotelOrderRepository.save(dbHotelOrder);
	}

	/**
	 * 个人中心通过登录人申请酒店退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateRefundState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		THotelOrder dbHotelOrder = tHotelOrderRepository.findByID(iID);
		dbHotelOrder.setOrderState((byte) 4);
		dbHotelOrder.setRefundProcess((byte) 1);
		dbHotelOrder.setModifyDate(new Date());
		dbHotelOrder.setReviser(sessionSysUser.getUserName());
		tHotelOrderRepository.save(dbHotelOrder);
	}

	/**
	 * 个人中心通过登录人点击撤销酒店退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateCancelState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		THotelOrder dbHotelOrder = tHotelOrderRepository.findByID(iID);
		dbHotelOrder.setOrderState((byte) 2);
		dbHotelOrder.setRefundProcess((byte) 0);
		dbHotelOrder.setModifyDate(new Date());
		dbHotelOrder.setReviser(sessionSysUser.getUserName());
		tHotelOrderRepository.save(dbHotelOrder);
	}

	/**
	 * 通过订单ID查询
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public THotelOrder findByID(int iID) {
		// TODO Auto-generated method stub
		return tHotelOrderRepository.findByID(iID);
	}

	/**
	 * 获取最大流水号
	 * 
	 * @return 返回值说明 流水号
	 * @author 创建人 dingxd
	 * @date 创建日期 2020.11.17
	 */
	public int getMaxID() {
		// 流水号
		Object objCode = tHotelOrderRepository.getMaxID();
		int iID = 0;
		if (objCode != null) {
			iID = Integer.parseInt(objCode.toString());
		}
		return iID;
	}

	/**
	 * 获取订单最大编码
	 * 
	 * @param 参数说明 计划单编码
	 * @return 返回值说明 订单编码
	 * @author 创建人 dingxd
	 * @date 创建日期 2020.11.17
	 */
	public String getMaxCode(int iPlanID) {
		// int 转 string
		String strPlanID = String.valueOf(iPlanID);
		// 返回订单最大编码
		Object objCode = tHotelOrderRepository.getMaxCode(strPlanID);
		String strCode = "";
		if (objCode != null) {
			strCode = objCode.toString();
		}
		return strCode;
	}

	/**
	 * 前台景区电子票创建订单
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2020.12.29
	 */
	public Map<String, Object> createOrderHotel(String strHotelPlan, String strHotelOrder, String strUseDate,
			TSysUser sessionSysUser) {
		// 处理order
		List<THotelOrder> lstHotelOrder = JSON.parseArray(strHotelOrder, THotelOrder.class);

		// 处理date
		Calendar cal = Calendar.getInstance();
		String date = strUseDate.replace("-", "");
		Integer year = Integer.parseInt(date.substring(0, 4));
		Integer month = Integer.parseInt(date.substring(4, 6));
		Integer day = Integer.parseInt(date.substring(6, 8));
		cal.set(year, month - 1, day, 23, 59, 59);
		Date useDate = cal.getTime();

		// 获取总额
		BigDecimal total = new BigDecimal("0");
		Integer orderNumber = new Integer(0);
		List<THotelPlan> lstHotelPlan = JSON.parseArray(strHotelPlan, THotelPlan.class);
		for (THotelPlan tHotelPlan : lstHotelPlan) {
			THotelPlan dbHotelPlan = tHotelPlanRepository.findByPlanID(tHotelPlan.getPlanID());
			BigDecimal price = dbHotelPlan.getPrice();
			Integer num = tHotelPlan.getNumber();
			// 订单根据预留数量减少
			int count = dbHotelPlan.getNumber() - num;
			dbHotelPlan.setNumber(count);
			dbHotelPlan.setModifyDate(new Date());
			dbHotelPlan.setReviser(sessionSysUser.getUserName());
			tHotelPlanRepository.save(dbHotelPlan);

			orderNumber = orderNumber + num;
			BigDecimal sum = price.multiply(BigDecimal.valueOf(num));
			total = total.add(sum);
		}
		Map<String, Object> data = new HashMap<>();
		List<Integer> lstID = new ArrayList<Integer>();
		for (THotelOrder tHotelOrder : lstHotelOrder) {
			int iPlanID = Integer.valueOf(tHotelOrder.getOrderID());
			// 获取 流水号
			tHotelOrder.setID(common.createMaxID(UtilConstants.TableName.T_HOTEL_ORDER));
			// 获取 订单号
			String cOrderID = common.createOrderCode(UtilConstants.TableName.T_HOTEL_ORDER, iPlanID);
			tHotelOrder.setOrderID(cOrderID);
			// 订单ID已存在=planID
			// 预留操作人
			tHotelOrder.setReserveUserID(sessionSysUser.getUserID());
			// 预留人姓名
			tHotelOrder.setReserveName(sessionSysUser.getUserName());
			// 预留人手机号
			tHotelOrder.setReserveTel(sessionSysUser.getUserMobile());
			// 预留日期
			tHotelOrder.setReserveDate(new Date());
			// 订单操作人
			tHotelOrder.setOrderUserID(sessionSysUser.getUserID());
			// 订单日期
			tHotelOrder.setOrderDate(new Date());
			// 订单人姓名、订单人电话、身份证号已存在
			// 同行人问题
			if (sessionSysUser.getUserType() == (byte) 0) {

				// 第一步查询身份证号码是否存在
				TSysUser dbSysUser = tSysUserRepository.findByIdentity(tHotelOrder.getIdentity());
				// 获得用户ID
				TSysUser tSysUser = new TSysUser();
				int iUserID = 0;
				// 如果等于空 在用户表里创建该用户
				if (dbSysUser == null) {

					tSysUser.setUserID(common.createBaseCode(UtilConstants.TableName.T_SYS_USER));
					// 默认账号和密码
					String strUserAccount = "visitor" + tSysUser.getUserID();
					tSysUser.setUserAccount(strUserAccount);
					tSysUser.setPassWord("123456");
					tSysUser.setUserName(tHotelOrder.getOrderName());
					tSysUser.setIdentity(tHotelOrder.getIdentity());
					// 用户推荐人为登录人
					tSysUser.setUpUserID(sessionSysUser.getUserID());
					// 用户所属公司为登陆人所属公司
					tSysUser.setOrgID(sessionSysUser.getOrgID());
					tSysUser.setCreator(sessionSysUser.getUserName());
					tSysUser.setCreateDate(new Date());
					tSysUserRepository.save(tSysUser.getUserID());
					tSysUserRepository.save(tSysUser);
					// 如果为空 获取创建的用户的ID
					iUserID = tSysUser.getUserID();
				} else {

					// 如果不为空，获得用户的ID
					iUserID = dbSysUser.getUserID();
				}

				// 查询同行人表中是否已存在该出行人和同行人
				TSysTogether dbSysTogether = tSysTogetherRepository.findByUserIDAndPeerID(sessionSysUser.getUserID(),
						iUserID);
				// 如果为空 创建同行人表
				if (dbSysTogether == null) {
					// 获得用户ID
					TSysTogether tSysTogether = new TSysTogether();

					tSysTogether.setID(common.createMaxID(UtilConstants.TableName.T_SYS_TOGETHER));
					tSysTogether.setUserID(sessionSysUser.getUserID());
					tSysTogether.setPeerID(iUserID);

					tSysTogether.setCreator(sessionSysUser.getUserName());
					tSysTogether.setCreateDate(new Date());
					tSysTogetherRepository.save(tSysTogether.getID());
					tSysTogetherRepository.save(tSysTogether);
				}
			}
			// 订单状态为1 正式订单
			tHotelOrder.setOrderState((byte) 1);
			// 订单数量
			tHotelOrder.setOrderNumber(orderNumber);
			// 订单总额
			tHotelOrder.setOrderAmount(total);
			// 订单使用日期
			tHotelOrder.setOrderInDate(useDate);
			// 订单底价、同业价、售价 从计划里获取
			THotelPlan dbScenicPlan = tHotelPlanRepository.findByPlanID(iPlanID);
			tHotelOrder.setFloor(dbScenicPlan.getFloor());
			tHotelOrder.setPeer(dbScenicPlan.getPeer());
			tHotelOrder.setPrice(dbScenicPlan.getPrice());
			// 创建登录人登录时间
			tHotelOrder.setCreator(sessionSysUser.getUserName());
			tHotelOrder.setCreateDate(new Date());
			// 创建订单
			tHotelOrderRepository.save(tHotelOrder.getID());
			tHotelOrderRepository.save(tHotelOrder);
			lstID.add(tHotelOrder.getID());
		}
		data.put("lstID", lstID);
		return data;
	}

	/**
	 * 函数功能说明：收据提交
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 2021.01.12
	 */
	public List<THotelOrder> updateHotelOrder(THotelOrder tHotelOrder, TSysUser sessionSysUser, String lstID) {
		// 拆分字符串
		String[] arrID = lstID.split(",");
		List<THotelOrder> lstHotelOrder = new ArrayList<THotelOrder>();
		for (int i = 0; i < arrID.length; i++) {
			// 定义一个新的实体类对象
			THotelOrder dbHotelOrder = tHotelOrderRepository.findByID(Integer.parseInt(arrID[i]));
			dbHotelOrder.setPayType(tHotelOrder.getPayType());

			dbHotelOrder.setReceivable(tHotelOrder.getReceivable());
			dbHotelOrder.setVoucher(tHotelOrder.getVoucher());
			dbHotelOrder.setReceipt(tHotelOrder.getReceipt());
			if (tHotelOrder.getReceivable() == tHotelOrder.getReceipt()) {
				dbHotelOrder.setOrderState((byte) 2);
			}
			dbHotelOrder.setModifyDate(new Date());
			dbHotelOrder.setReviser(sessionSysUser.getUserName());
			tHotelOrderRepository.save(dbHotelOrder);
			lstHotelOrder.add(dbHotelOrder);
		}
		return lstHotelOrder;
	}

}
